<script setup lang="ts">
	import { onLaunch, onShow, onHide, onExit } from "@dcloudio/uni-app";
	import { usePlayerStore } from "@/stores/usePlayerStore";
	// #ifdef APP-PLUS
	import { acquireWakeLock, releaseWakeLock, isOpenWakeLock } from '@/uni_modules/qd-wakeLock';
	// #endif

	const playerStore = usePlayerStore();
	onLaunch(() => {
		// #ifdef APP-PLUS
		plus.nativeUI.setUIStyle('dark');
		acquireWakeLock();
		uni.showToast({ title: `wakelock=${isOpenWakeLock()}`, duration: 1000 })
		// #endif
		if (playerStore.autoPlay) {
			// #ifdef H5
			uni.showToast({ title: '请戴上耳机' });
			// #endif
			playerStore.play();
		} else playerStore.stop();

		// #ifdef APP-PLUS
		const AudioNotification = uni.requireNativePlugin("Audio-Notification");
		AudioNotification.showView({
			title: playerStore.icyData.song || playerStore.currentRadio.name,
			singer: '',
			image: playerStore.icyData.cover || playerStore.currentRadio.favicon
		},
			(res : { btn : any; }) => {
				switch (res.btn) {
					case 0: // 点击了上一曲 
						playerStore.prev();
						break;
					case 1: // 点击了暂停按钮 
						playerStore.stop(); break;
					case 2: // 点击了播放按钮 
						playerStore.play(); break;
					case 3: // 点击了下一曲 
						playerStore.next(); break;
					case 4: // 点击了关闭按钮
						AudioNotification.hideView()
						break
				}
			})
		// #endif 
	});

	onShow(() => {
		console.log("App Show");
	});
	onHide(() => {
		console.log("App Hide");
	});
	onExit(() => {
		console.log('App exit');
		playerStore.stop();
		// #ifdef APP-ANDROID
		console.log('关闭wake lock');
		releaseWakeLock();
		const AudioNotification = uni.requireNativePlugin("Audio-Notification");
		AudioNotification.hideView();
		// #endif
	})
</script>
<style>
	@font-face {
		font-family: 'iconfont';
		src: url('@/static/iconfont.ttf');
	}
</style>